package com.jcp.daoimp;

import java.util.List;

import com.jcp.dao.userAndFileDao;
import com.jcp.entity.Departments;
import com.jcp.entity.FilesAndUsers;
import com.jcp.util.DBUtil;

public class userAddFileDao implements userAndFileDao {
	/**
	 * 员工管理的添加功能
	 * @param FilesAndUsers的实体类
	 * @return 布尔值
	 */
	@Override
	public boolean userAdd(FilesAndUsers fau,String userPwd) {
		//对users进行添加的功能
		String sql = "insert into users(userName,userEmail,cardId,deptId,posId,userPwd)values(?,?,?,?,2,?)";
		//对档案进行添加的功能
		String sql2 = "insert into files(sex,phone,address,cardId)values(?,?,?,?)";
		String sql3 = "select * from departments where deptName=?";
		//获取部门的集合对象
		List<Departments> list = DBUtil.exQuery(sql3, Departments.class,fau.getDeptName());
		System.out.println(list);
		//对判断是技术部还是开发部
/*		int DeptId=1;
		if(fau.getDeptName().equals("技术部")) {
			DeptId=1;
		}else if(fau.getDeptName().equals("开发部")) {
			DeptId=2;
		}*/
		//users表和档案表的返回值并加以判断
		int listUser = DBUtil.exUpdate(sql, fau.getUserName(),fau.getUserEmail(),fau.getCardId(),list.get(0).getDeptId(),userPwd);
		int listFile = DBUtil.exUpdate(sql2, fau.getSex(),fau.getPhone(),fau.getAddress(),fau.getCardId());
		
		if(listUser>0&&listFile>0) {
			return true;
		}else {
			return false;
		}
	}
	/**
	 * 员工管理的编辑功能
	 * @param FilesAndUsers的实体类
	 * @return 布尔值
	 */
	@Override
	public boolean userUpdate(FilesAndUsers fau) {
		String sql = "update users set userName=?,userEmail=?,cardId=?,deptId=? where userId=?";
		String sql2 = "update files set phone=?,address=? where cardId=? ";
		String sql3 = "select * from departments where deptName=?";
		//获取部门的集合对象
		List<Departments> list = DBUtil.exQuery(sql3, Departments.class,fau.getDeptName());
		//修改员工的基本信息
		int listUser = DBUtil.exUpdate(sql, fau.getUserName(),fau.getUserEmail(),fau.getCardId(),list.get(0).getDeptId(),fau.getUserId());
		//修改档案的基本信息
		int listFile = DBUtil.exUpdate(sql2, fau.getPhone(),fau.getAddress(),fau.getCardId());
		if(listUser>0&&listFile>0) {
			return true;
		}else {
			return false;
		}
	}
	/**
	 * 员工管理的删除功能
	 * @param cardId：身份证
	 * @return 布尔值
	 */
	@Override
	public boolean userDel(int cardId) {
		String sql = "delete from users where cardId=?";
		String sql2 = "delete from files where cardId=? ";

		int listUser = DBUtil.exUpdate(sql,cardId);
		int listFile = DBUtil.exUpdate(sql2,cardId);
		if(listUser>0&&listFile>0) {
			return true;
		}else {
			return false;
		}
	}
	/**
	 * 部门的查询功能
	 */
	@Override
	public List<Departments> postSel() {
		String sql = "select * from departments";
		return DBUtil.exQuery(sql,Departments.class);
	}

}
